/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.nuage.tiow.ui.warhost;

import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Table;
import com.vaadin.ui.Window.Notification;
import java.util.ArrayList;
import java.util.Collection;
import org.nuage.tiow.vo.warhost.Warhost;
import java.util.List;
import org.nuage.tiow.ui.UnitItem;
import org.nuage.tiow.vo.warhost.Unit;

/**
 *
 * @author quentina
 */
public class WarhostEditor extends Panel {

    public WarhostEditor(final Warhost warhost) {
        setCaption("Warhost " + warhost.getName());
        final IndexedContainer container = getContainer(warhost);
        final Table table = new Table("Warhost entries", container);
        table.setVisibleColumns(new Object[]{"name", "type", "description", "namedCharacter", "independentCharacter", "points"});
        table.setEditable(false);
        table.setSelectable(true);
        table.setImmediate(true);

        HorizontalLayout menu = new HorizontalLayout();
        addComponent(menu);

        menu.addComponent(new Button("Add unit", new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                container.addItem();
                table.setEditable(true);
            }
        }));
        menu.addComponent(new Button("Toggle edit", new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                table.setEditable(!table.isEditable());
            }
        }));
        menu.addComponent(new Button("Remove unit", new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                Object value = table.getValue();
                if (value != null) {
                    table.removeItem(value);
                }
            }
        }));
        menu.addComponent(new Button("Save", new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                getWindow().showNotification("Save", "Mock save", Notification.TYPE_TRAY_NOTIFICATION);
            }
        }));


        addComponent(table);
    }

    private IndexedContainer getContainer(Warhost warhost) {
        IndexedContainer container = new IndexedContainer();
        container.addContainerProperty("name", String.class, "");
        container.addContainerProperty("type", String.class, "");
        container.addContainerProperty("description", String.class, "");
        container.addContainerProperty("namedCharacter", Boolean.class, false);
        container.addContainerProperty("independentCharacter", Boolean.class, false);
        container.addContainerProperty("points", Integer.class, 0);
        for (Unit unit : warhost.getUnits()) {
            Item item = container.addItem(unit);
            item.getItemProperty("name").setValue(unit.getName());
            item.getItemProperty("type").setValue(unit.getType());
            item.getItemProperty("description").setValue(unit.getDescription());
            item.getItemProperty("namedCharacter").setValue(unit.isNamedCharacter());
            item.getItemProperty("independentCharacter").setValue(unit.isIndependentCharacter());
            item.getItemProperty("points").setValue(unit.getPoints());
        }
        return container;
    }

    private List<UnitItem> getUnitItems(Warhost warhost) {
        List<UnitItem> items = new ArrayList<UnitItem>();
        List<Unit> units = warhost.getUnits();
        for (Unit unit : units) {
            items.add(new UnitItem(unit));
        }
        return items;
    }
}
